var randomStr = Date.now() + parseInt(Math.random() * 100);
var waitTime = 60;
$(function() {
	validateRule();
	getTenant();
	$('#btnReset').click(function(){
		window.location.href = '/';
	});
	
	$('#sendFpValidCode').click(function() {
		var $this = this;
		var loginname = $.common.trim($("input[name='loginname']").val());
		var tenantId = $.common.trim($("select[id='tenantSelect']").val());
		if(loginname == '') {
			$.modal.alertWarning("请输入登录名");
			return false;
		}
		if(tenantId == '') {
			$.modal.alertWarning('请选择租户');
			return false;
		}
		sendTime($this);
		$.ajax({
			type : "get",
			url : api + '/admin/emailCode/' + loginname + '/' + tenantId,
			headers: {
			  "TENANT-ID": tenantId,
			},
			success : function(r) {
				if (r.code == 0) {
					$.modal.msg("验证码已发送！");
				} else {
					$.modal.closeLoading();
					$.modal.msg(r.msg);
					waitTime = 0;
				}
			}
		});
	});
});

$.validator.setDefaults({
	submitHandler : function() {
		if($.validate.form()) {
			register();
		}
	}
});

function sendTime(o) {
	if (waitTime == 0) {
		o.removeAttribute("disabled");
		o.value = "获取验证码";
		waitTime = 60;
	} else {
		o.setAttribute("disabled", true);
		o.value = "重新发送(" + waitTime + ")";
		waitTime--;
		setTimeout(function() {
			sendTime(o)
		}, 1000);
	}
}

function getTenant() {
  $.ajax({
    type: "get",
    url: api + "/admin/tenant/list",

    success: function (res) {
      var options = []
      res.data.forEach(function (item) {
        options.push('<option value=' + item.id + '>' + item.name + '</option>')
      })
      $('#tenantSelect').append(options)
      if (storage.get('tenantId')) {
        $('#tenantSelect').val(storage.get('tenantId'))
      }
    }
  });
}

function register() {
	$.modal.loading($("#btnSubmit").data("loading"));
	var loginname = $.common.trim($("input[name='loginname']").val());
	var tenantId = $.common.trim($("select[id='tenantSelect']").val());
	var newpassword1 = algorithmRequestParam($.common.trim($("input[name='password']").val()));
	var checkCode = $.common.trim($("input[name='fpValidCode']").val());
	var obj = {
			"loginname" : loginname,
			"tenantId" : tenantId,
			"newpassword1" : newpassword1,
			"checkCode" : checkCode
		};
	$.ajax({
		type : "put",
		url : api + "/admin/user/forgetpwd",
		headers: {
		  "TENANT-ID": tenantId,
		  "Content-Type": "application/json;charset=UTF-8"
		},
		data : JSON.stringify(obj),
		success : function(r) {
			$.modal.closeLoading();
			if (r.code == 0) {
				layer.alert("<font color='red'>密码重置成功</font>", {
					icon : 1,
					title : "系统提示"
				}, function(index) {
					// 关闭弹窗
					layer.close(index);
					window.location.href = '/';
				});
			} else {
				$.modal.msg(r.msg);
			}
		}
	});
}

function validateRule() {
	var icon = "<i class='fa fa-times-circle'></i> ";
	$("#forgetForm").validate({
		rules : {
			loginname : {
				required : true
			},
			password : {
				required : true,
				minlength : 6
			},
			confirmPassword : {
				required : true,
				equalTo : "[name='password']"
			},
			fpValidCode : {
				required : true,
				minlength : 4,
				maxlength : 4
			}
		},
		messages : {
			loginname : {
				required : icon + "请输入您的登录名"
			},
			password : {
				required : icon + "请输入您的密码",
				minlength : icon + "密码不能小于6个字符",
			},
			confirmPassword : {
				required : icon + "请再次输入您的密码",
				equalTo : icon + "两次密码输入不一致"
			},
			fpValidCode : {
				required : icon + "请输入邮箱验证码",
				minlength : icon + "请输入4位数的验证码",
				maxlength : icon + "请输入4位数的验证码"
			}
		}
	})
}
